package cn.lj.exception;


import cn.lj.response.Result;
import cn.lj.response.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @file: GlobalExceptionHandler
 * @version:
 * @Description: 全局异常处理
 * @author: 李津
 * @date: 2021-04-13 15:53
 * 本代码要注意的事项、备注事项等
 */
//log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl
@RestControllerAdvice
@Slf4j
public class LJExceptionHandler {

    /**
     * 权限不足异常
     * @param e
     * @return
     */
    @ResponseStatus(HttpStatus.FORBIDDEN)
    @ExceptionHandler(value = AccessDeniedException.class)
    public Result handler(AccessDeniedException e) {
        log.info("security权限不足：----------------{}", e.getMessage());
        return Result.error(ResultCode.NO_PERMISSION);
    }

    /**
     * 实体校验异常捕获
     * @param e
     * @return
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public Result handler(MethodArgumentNotValidException e) {
        BindingResult result = e.getBindingResult();
        ObjectError objectError = result.getAllErrors().stream().findFirst().get();
        log.error("实体校验异常：----------------{}", objectError.getDefaultMessage());
        return Result.error(ResultCode.OBJECT_CHECK);
    }

    /**
     * 抛出该错误以指示方法已传递了非法或不适当的参数
     * @param e
     * @return
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = IllegalArgumentException.class)
    public Result handler(IllegalArgumentException e) {
        log.error("Assert异常：----------------{}", e.getMessage());
        return Result.error(ResultCode.PARAM_NOT_VALID);
    }

    /**
     * 全局异常处理,没有指定异常的类型,不管什么异常都是可以捕获的
     *
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public Result error(Exception e) {
        e.printStackTrace();
        return Result.error();
    }

    /**
     * 处理特定异常类型,可以定义多个,这里只以ArithmeticException为例
     *
     * @param e
     * @return
     */
    @ExceptionHandler(ArithmeticException.class)
    public Result error(ArithmeticException e) {
        //e.printStackTrace();
        log.error(e.getMessage());
        return Result.error().code(ResultCode.ARITHMETIC_EXCEPTION.getCode())
                .message(ResultCode.ARITHMETIC_EXCEPTION.getMessage());
    }

    /**
     * 处理业务异常,自己定义的异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(BusinessException.class)
    public Result error(BusinessException e) {
        //e.printStackTrace();
        log.error(e.getErrMsg());
        return Result.error().code(e.getCode())
                .message(e.getErrMsg());
    }

}
